$(document).ready(function() {
	$('#s-by-name-btn').click(function(){
		var leksName = $('#s-by-name').val();
		var currLang = $('#hid-filter-lang').val();
		$.ajax({
   			type: "post",
   			url: "index.php?cont=ajax&act=search",
   			data: "name="+leksName+"&lang="+currLang,
   			success: proccess
 		});
	})
	
	$('#s-by-val-btn').click(function(){
		var leksVal = $('#s-by-val').val();
		var currLang = $('#hid-filter-lang').val();
		$.ajax({
   			type: "post",
   			url: "index.php?cont=ajax&act=searchval",
   			data: "value="+leksVal+"&lang="+currLang,
   			success: proccess
 		});
	})
	
	proccess = function(data) {
		var leks = eval('(' + data + ')');
		var tbody = document.getElementById('leks-table').getElementsByTagName('TBODY')[0];
		while(tbody.rows.length > 0) {
			tbody.removeChild(tbody.lastChild);
		}
		for (var i = 0; leks.length; i++) {
			if(leks[i]) {
				var tr = document.createElement('TR');
				tbody.appendChild(tr);
				var tdNum = document.createElement('TD');
				var tdName = document.createElement('TD');
				var tdBv = document.createElement('TD');
                var tdCont = document.createElement('TD');
				var tdStat = document.createElement('TD');
				var tdMan = document.createElement('TD');
				var tdAct = document.createElement('TD');
				tr.appendChild(tdNum);
				tr.appendChild(tdName);
				tr.appendChild(tdBv);
                tr.appendChild(tdCont);
				tr.appendChild(tdStat);
				tr.appendChild(tdMan);
				tr.appendChild(tdAct);
				tdNum.innerHTML = i+1;
				tdName.innerHTML = leks[i].name;
				tdBv.innerHTML = leks[i].value;
                if(leks[i].context != '') {
                    tdCont.innerHTML = leks[i].context;
                }
                else {
                    tdCont.innerHTML = "no context";
                }
				if((leks[i].translated) != 0) {
					tr.setAttribute('class', 'colored-transl');
					tdStat.innerHTML = 'translated';
					tdMan.innerHTML = '<a href="index.php?cont=leks&amp;act=view&amp;id='+leks[i].id+'" class="view-trans">view translations</a>/<a href="index.php?cont=leks&amp;act=translate&amp;id='+leks[i].id+'">translate</a></td>';
				}
				else {
					tr.setAttribute('class', 'colored');
					tdStat.innerHTML = 'not translated';
					tdMan.innerHTML = '<a href="index.php?cont=leks&amp;act=translate&amp;id='+leks[i].id+'">translate</a>';
				}
				tdAct.innerHTML = '<a href="index.php?cont=leks&amp;act=edit&amp;id='+leks[i].id+'">&nbsp;<img src="misc/img/edit.png" alt="edit leksem" border="0" /></a>&nbsp;|&nbsp;<a href="index.php?cont=leks&amp;act=delete&amp;id='+leks[i].id+'"><img src="misc/img/delete.png" alt="delete leksem" border="0" /></a>';
			}
			else {
				break;
			}
		}
	}
});